import java.util.*;
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {//暴力创建一棵二叉树

        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }

    public void preOrder(TreeNode root){//前序遍历
        if(root==null) return ;
        System.out.println(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public void inOrder(TreeNode root){//中序遍历
        if(root==null) return ;
        preOrder(root.left);
        System.out.println(root.val+" ");
        preOrder(root.right);
    }

    public void postOrder(TreeNode root){//后序遍历
        if(root==null) return ;
        preOrder(root.left);
        preOrder(root.right);
        System.out.println(root.val+" ");
    }

    int treeSize(TreeNode root){//获取树节点数
        if(root==null) return 0;
        return treeSize(root.left)+treeSize(root.right)+1;
    }

    int getLeafNodeCount(TreeNode root){//获取叶子节点数
        if(root == null) return 0;
        if(root.left == null && root.right == null) return 1;
        return getLeafNodeCount(root.left)+getLeafNodeCount(root.right);
    }

    int getKLevelNodeCount(TreeNode root,int k){//获取第k层节点数
        if(root == null) return 0;
        if(k == 1) return 1;
        return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);
    }

    int getHeight(TreeNode root){//获取二叉树高度
        if(root==null) return 0;
        return Math.max(getHeight(root.left),getHeight(root.right))+1;
    }

    TreeNode find(TreeNode root,int val){//查找某节点
        if(root == null) return null;
        if(root.val == val) return root;//找到后返回
        TreeNode ret=find(root.left,val);
        if(ret!=null){//找到后返回
            return ret;
        }
        ret=find(root.right,val);
        if(ret!=null){//找到后返回
            return ret;
        }
        return null;
    }

    List<List<Character>> levelOrder(TreeNode root){//层序遍历
        List<List<Character>> ret = new ArrayList<>();
        if(root == null) return ret;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.println(cur.val+"");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
        return ret;
    }


}
